<!-- eslint-disable vue/valid-v-slot -->
<!-- eslint-disable vue/no-lone-template -->
<template>
  <div class="main-container">
    <TableBody>
      <template #header>
        <TableHeader ref="tableHeaderRef" :show-filter="false">
          <template #search-content>
            <a-form ref="seachForm" :model="form">
              <a-row :gutter="24">
                <a-col :span="4">
                  <a-form-item field="card" hide-label>
                    <a-input
                      class="width100"
                      v-model="form.card"
                      placeholder="请输入卡号"
                      allow-clear
                    />
                  </a-form-item>
                </a-col>
                <a-col :span="4">
                  <a-form-item field="phone" hide-label>
                    <a-input
                      class="width100"
                      v-model="form.phone"
                      placeholder="请输入手机号"
                      allow-clear
                    />
                  </a-form-item>
                </a-col>
                <a-col :span="4">
                  <a-form-item field="state" hide-label>
                    <a-select v-model="form.state" placeholder="请选择投诉状态" allow-clear>
                      <a-option value="0">待处理</a-option>
                      <a-option value="1">处理中</a-option>
                      <a-option value="2">已处理</a-option>
                      <!-- <a-option value="3">体验包</a-option> -->
                    </a-select>
                  </a-form-item>
                </a-col>
                <a-col :span="4">
                  <a-form-item field="addTime" hide-label>
                    <a-date-picker
                      class="width100"
                      v-model="form.addTime"
                      show-time
                      :disabledDate="changeTraceEnd"
                      placeholder="请选择开始时间"
                      :time-picker-props="{ defaultValue: '12:00:00' }"
                      format="YYYY-MM-DD HH:mm:ss"
                      :shortcuts="[
                        {
                          label: '今日',
                          value: () => (
                            (form.addTime = $dayjs('00:00:00', 'HH:mm:ss').format(
                              'YYYY-MM-DD HH:mm:ss'
                            )),
                            (form.endTime = $dayjs('23:59:59', 'HH:mm:ss').format(
                              'YYYY-MM-DD HH:mm:ss'
                            ))
                          ),
                        },
                        {
                          label: '昨日',
                          value: () => (
                            (form.addTime = $dayjs('00:00:00', 'HH:mm:ss')
                              .subtract(1, 'day')
                              .format('YYYY-MM-DD HH:mm:ss')),
                            (form.endTime = $dayjs('23:59:59', 'HH:mm:ss')
                              .subtract(1, 'day')
                              .format('YYYY-MM-DD HH:mm:ss'))
                          ),
                        },
                        {
                          label: '七日',
                          value: () => (
                            (form.addTime = $dayjs('00:00:00', 'HH:mm:ss')
                              .subtract(6, 'day')
                              .format('YYYY-MM-DD HH:mm:ss')),
                            (form.endTime = $dayjs().format('YYYY-MM-DD HH:mm:ss'))
                          ),
                        },
                        {
                          label: '本月',
                          value: () => (
                            (form.addTime = $dayjs('01 00:00:00', 'DD HH:mm:ss').format(
                              'YYYY-MM-DD HH:mm:ss'
                            )),
                            (form.endTime = $dayjs().format('YYYY-MM-DD HH:mm:ss'))
                          ),
                        },
                        {
                          label: '上月',
                          value: () => (
                            (form.addTime = $dayjs(`01 00:00:00`, 'DD HH:mm:ss')
                              .subtract(1, 'month')
                              .format('YYYY-MM-DD HH:mm:ss')),
                            (form.endTime = $dayjs()
                              .subtract(1, 'month')
                              .endOf('month')
                              .format('YYYY-MM-DD HH:mm:ss'))
                          ),
                        },
                      ]"
                    />
                  </a-form-item>
                </a-col>

                <a-col :span="4">
                  <a-form-item field="endTime" hide-label>
                    <a-date-picker
                      class="width100"
                      v-model="form.endTime"
                      show-time
                      :disabledDate="changeTraceStart"
                      placeholder="请选择结束时间"
                      :time-picker-props="{ defaultValue: '12:00:00' }"
                      :shortcuts="[
                        {
                          label: '今日',
                          value: () => (
                            (form.addTime = $dayjs('00:00:00', 'HH:mm:ss').format(
                              'YYYY-MM-DD HH:mm:ss'
                            )),
                            (form.endTime = $dayjs('23:59:59', 'HH:mm:ss').format(
                              'YYYY-MM-DD HH:mm:ss'
                            ))
                          ),
                        },
                        {
                          label: '昨日',
                          value: () => (
                            (form.addTime = $dayjs('00:00:00', 'HH:mm:ss')
                              .subtract(1, 'day')
                              .format('YYYY-MM-DD HH:mm:ss')),
                            (form.endTime = $dayjs('23:59:59', 'HH:mm:ss')
                              .subtract(1, 'day')
                              .format('YYYY-MM-DD HH:mm:ss'))
                          ),
                        },
                        {
                          label: '七日',
                          value: () => (
                            (form.addTime = $dayjs('00:00:00', 'HH:mm:ss')
                              .subtract(6, 'day')
                              .format('YYYY-MM-DD HH:mm:ss')),
                            (form.endTime = $dayjs().format('YYYY-MM-DD HH:mm:ss'))
                          ),
                        },
                        {
                          label: '本月',
                          value: () => (
                            (form.addTime = $dayjs('01 00:00:00', 'DD HH:mm:ss').format(
                              'YYYY-MM-DD HH:mm:ss'
                            )),
                            (form.endTime = $dayjs().format('YYYY-MM-DD HH:mm:ss'))
                          ),
                        },
                        {
                          label: '上月',
                          value: () => (
                            (form.addTime = $dayjs(`01 00:00:00`, 'DD HH:mm:ss')
                              .subtract(1, 'month')
                              .format('YYYY-MM-DD HH:mm:ss')),
                            (form.endTime = $dayjs()
                              .subtract(1, 'month')
                              .endOf('month')
                              .format('YYYY-MM-DD HH:mm:ss'))
                          ),
                        },
                      ]"
                    />
                  </a-form-item>
                </a-col>
                <a-col :span="4">
                  <a-form-item field="btns" hide-label>
                    <a-space :size="15">
                      <a-button
                        type="primary"
                        v-permission="deleteList"
                        html-type="submit"
                        @click="doRefresh(1)"
                        ><template #icon> <icon-search /> </template>搜索</a-button
                      >
                      <a-button v-permission="deleteList" @click="resetSeach"
                        ><template #icon>
                          <icon-sync theme="outline" size="14" />
                        </template>
                        <!-- Use the default slot to avoid extra spaces -->
                        <template #default>重置</template></a-button
                      >
                    </a-space>
                  </a-form-item>
                </a-col>
              </a-row>
            </a-form>
          </template>
        </TableHeader>
      </template>
      <template #table-config>
        <a-space :size="15">
          <a-button
            type="outline"
            v-permission="exportDeleteList"
            @click="exportExcelLists"
            :loading="loading"
          >
            <!-- Use the default slot to avoid extra spaces -->
            <template #default>导出</template>
          </a-button>
          <a-button @click="orderHandle" type="outline">
            <template #default>批量处理</template>
          </a-button>
          <a-button
            type="primary"
            style="width: 75px; height: 30px; font-size: 13px; margin-left: 0px"
            size="mini"
            @click="openUrl"
            status="warning"
          >
            投诉链接</a-button
          >
        </a-space>
        <a-space :size="15">
          <TableConfig
            @update-border="onUpdateBorder"
            @update-striped="onUpdateStriped"
            @refresh="doRefresh"
          />
        </a-space>
      </template>
      <template #default>
        <a-table
          :bordered="{ wrapper: true, cell: tables.bordered }"
          :stripe="(tables.striped as boolean)"
          :loading="tables.tableLoading"
          :data="table.dataList"
          :pagination="false"
          :column-resizable="true"
          :rowKey="rowKey"
          :columns="tableColumns.list"
          :row-selection="rowSelection"
          table-layout-fixed
          :scroll="{ x: 1500 }"
          :scrollbar="{ type: 'track', outerClass: '', outerStyle: '' }"
          v-model:selectedKeys="selectedKeys"
          hide-expand-button-on-empty
          :default-expand-all-rows="true"
        >
          <template #columns>
            <a-table-column
              v-for="item of tableColumns.list"
              :key="item.key"
              align="left"
              :title="(item.title as string)"
              :width="item.width"
              :data-index="(item.key as string)"
              :fixed="item.fixed"
              :ellipsis="true"
              :tooltip="true"
            >
              <template v-if="item.key === 'img'" #cell="{ record }">
                <a-space>
                  <a-image-preview-group infinite>
                    <a-space>
                      <a-image v-for="(itemImg, index) in record.img" :src="itemImg" width="50" />
                    </a-space>
                  </a-image-preview-group>
                </a-space>
              </template>
              <template v-if="item.key === 'state'" #cell="{ record }">
                <a-tag color="blue" v-if="record.state == 2">已处理</a-tag>
                <a-tag color="gray" v-if="record.state == 0">待处理</a-tag>
                <a-tag color="gold" v-if="record.state == 1">处理中</a-tag>
              </template>
              <template v-if="item.key === 'action'" #cell="{ record }">
                <a-space>
                  <a-button size="small" type="text" class="butten" @click="chuli(record)">
                    处理
                  </a-button>
                  <a-button
                    size="small"
                    type="text"
                    status="danger"
                    class="butten"
                    @click="del(record)"
                  >
                    删除
                  </a-button>
                </a-space>
              </template>
            </a-table-column>
          </template>
        </a-table>
      </template>
      <template #footer>
        <TableFooter ref="tableFooterRef" :pagination="pagination" />
      </template>
    </TableBody>
    <a-modal
      v-model:visible="balancevisible"
      @before-close="handleCancelBan"
      width="500px"
      :mask-closable="false"
      title-align="start"
    >
      <template #title> 处理投诉 </template>
      <a-form ref="balanceForms" :auto-label-width="true" :model="banlanceForm">
        <a-form-item field="state" label="处理状态">
          <a-radio-group v-model="banlanceForm.state" class="radio-bcg">
            <a-radio :value="0">
              <template #radio="{ checked }">
                <div style="display: flex" :class="{ 'custom-radio-card-checked': checked }">
                  <div className="custom-radio-card-mask">
                    <div className="custom-radio-card-mask-dot"></div>
                  </div>
                  <div className="custom-radio-card-title">待处理</div>
                </div>
              </template>
            </a-radio>
            <a-radio :value="1">
              <template #radio="{ checked }">
                <div style="display: flex" :class="{ 'custom-radio-card-checked': checked }">
                  <div className="custom-radio-card-mask">
                    <div className="custom-radio-card-mask-dot"></div>
                  </div>
                  <div className="custom-radio-card-title">处理中</div>
                </div>
              </template>
            </a-radio>
            <a-radio :value="2">
              <template #radio="{ checked }">
                <div style="display: flex" :class="{ 'custom-radio-card-checked': checked }">
                  <div className="custom-radio-card-mask">
                    <div className="custom-radio-card-mask-dot"></div>
                  </div>
                  <div className="custom-radio-card-title">已处理</div>
                </div>
              </template>
            </a-radio>
          </a-radio-group>
        </a-form-item>

        <a-form-item field="handlerRemarks" label="处理备注" asterisk-position="end">
          <a-textarea v-model="banlanceForm.handlerRemarks" placeholder="请输入备注" allow-clear />
        </a-form-item>
      </a-form>
      <template #footer>
        <a-space>
          <a-button @click="handleCancelBan">取消</a-button>
          <a-button type="primary" @click="handleBeforeOkBan">确定</a-button>
        </a-space>
      </template>
    </a-modal>
    <a-modal
      v-model:visible="visiblesa"
      title-align="start"
      title="投诉链接"
      @before-close="beforeClose1"
    >
      <div style="display: flex; justify-content: center; margin: 20px">
        <a-image
          style="width: 170px; height: 170px; text-align: center"
          :src="imgUrl"
          show-loader
        />
      </div>
      <div style="display: flex; justify-content: center">
        <div
          style="
            width: 390px;
            background-color: #f2f3f5;
            border-radius: 4px;
            text-align: center;
            padding: 4px;
          "
        >
          <a id="bols" :href="url" style="color: #000000" target="_blank">
            {{ url }}
          </a>
        </div>
      </div>
      <div style="display: flex; justify-content: center; margin: 20px">
        <div style="display: flex; width: 390px; justify-content: space-between">
          <a-button type="primary" status="success" long @click="downs">保存二维码</a-button>
          <div style="width: 40px"></div>
          <a-button type="primary" long @click="userCommon.copy(url)">复制链接</a-button>
        </div>
      </div>
      <template #footer>
        <a-space />
      </template>
    </a-modal>
  </div>
</template>

<script lang="ts" setup>
  import {
    deleteList,
    exportDeleteList,
    recordtDeleteList,
    exportRecordDeleteList,
    handlerComplaint,
    qr,
    delComplaint,
  } from '@/api/url'
  import * as http from '@/api/http'
  import { get } from '@/api/http'
  import { Message, Modal } from '@arco-design/web-vue'
  import { usePagination, useRowKey, useTable } from '@/hooks/table'
  import { onMounted, reactive, ref } from 'vue'
  import useCommonStore from '@/store/modules/common'
  const table = useTable()
  const userCommon = useCommonStore()
  const pagination = usePagination(doRefresh)
  const seachForm = ref()
  const loading = ref(false)
  const loadings = ref(false)
  const id = ref()
  const success = ref()
  const rowSelection = reactive({
    type: 'checkbox',
    showCheckedAll: true,
    onlyCurrent: false,
  })
  const banlanceForm = ref({
    state: 0,
    handlerRemarks: '',
    ids: [],
  })
  const form = reactive({
    addTime: '',
    endTime: '',
    card: '',
    phone: '',
  })

  const tables = reactive({
    tableLoading: false,
    bordered: false,
    striped: false,
  })
  const balancevisible = ref(false)
  const tableColumns: any = reactive({
    list: [
      {
        title: '卡号',
        key: 'card',
        dataIndex: 'card',
        align: 'center',
        checked: true,
        width: 160,
      },
      {
        title: '手机号',
        key: 'phone',
        dataIndex: 'phone',
        align: 'center',
        checked: true,
        width: 160,
      },
      {
        title: '问题图片',
        key: 'img',
        dataIndex: 'img',
        align: 'center',
        checked: true,
        width: 200,
      },
      {
        title: 'IP',
        key: 'ip',
        dataIndex: 'ip',
        align: 'center',
        checked: true,
        width: 160,
      },
      {
        title: 'IP地址',
        key: 'address',
        dataIndex: 'address',
        align: 'center',
        checked: true,
        width: 160,
      },
      {
        title: '状态',
        key: 'state',
        dataIndex: 'state',
        align: 'center',
        checked: true,
        width: 100,
      },
      {
        title: '投诉原因',
        key: 'content',
        dataIndex: 'content',
        align: 'center',
        checked: true,
        width: 200,
      },
      {
        title: '处理备注',
        key: 'handlerRemarks',
        dataIndex: 'handlerRemarks',
        align: 'center',
        checked: true,
        width: 190,
      },
      {
        title: '投诉时间',
        key: 'addTime',
        dataIndex: 'addTime',
        align: 'center',
        checked: true,
        width: 190,
      },
      {
        title: '处理时间',
        key: 'handlerTime',
        dataIndex: 'handlerTime',
        align: 'center',
        checked: true,
        width: 190,
      },

      {
        title: '操作',
        key: 'action',
        dataIndex: 'action',
        align: 'center',
        checked: true,
        width: 120,
        fixed: 'right',
      },
    ],
  })
  const scroll = {
    x: 1500,
  }
  const rowKey = useRowKey('id')
  const selectedKeys = ref([])
  const handleCancelBan = () => {
    balancevisible.value = false
    banlanceForm.value = {
      state: 0,
      handlerRemarks: '',
      ids: [],
    }
    selectedKeys.value = []
  }
  function orderHandle() {
    if (selectedKeys.value.length) {
      console.log(selectedKeys)
      banlanceForm.value.state = 1
      banlanceForm.value.ids = selectedKeys.value
      balancevisible.value = true
    } else {
      Message.error('选中投诉不能为空')
    }
  }
  const handleBeforeOkBan = () => {
    http
      .get({
        url: handlerComplaint,
        data: () => {
          return {
            ...banlanceForm.value,
          }
        },
      })
      .then((res) => {
        if (res.code == 0) {
          Message.success(res.msg)
          balancevisible.value = false
          handleCancelBan()
          doRefresh()
        } else {
          Message.error(res.msg)
        }
      })
  }
  const visiblesa: any = ref(false)
  const url: any = ref('')
  const imgUrl: any = ref('')
  const openUrl = () => {
    url.value = window.location.protocol + '//' + window.location.host + '/pages/index/tousu'
    imgUrl.value =
      window.location.protocol +
      '//' +
      window.location.host +
      qr +
      '?content=' +
      encodeURIComponent(
        window.location.protocol + '//' + window.location.host + '/pages/index/tousu'
      )
    visiblesa.value = true
  }
  function beforeClose1() {
    visiblesa.value = false
  }
  function del(val: any) {
    Modal.warning({
      title: '操作确认',
      content: '确定要删除此投诉？',
      cancelText: '取消',
      titleAlign: 'start',
      hideCancel: false,
      okText: '删除',
      onOk: () => {
        get({
          url: '/app/complaint/delete',
          data: () => {
            return {
              id: val.id,
            }
          },
        })
          .then((res) => {
            if (res.code == 0) {
              Message.success(res.msg)
              doRefresh()
            } else {
              Message.error(res.msg)
            }
          })
          .catch(console.log)
      },
    })
  }
  const chuli = (val) => {
    console.log(val)
    balancevisible.value = true
    banlanceForm.value.ids = [val.id]
    banlanceForm.value.state = val.state
    if (val.handlerRemarks) {
      banlanceForm.value.handlerRemarks = val.handlerRemarks
    }
    console.log(banlanceForm.value)
  }
  const resetSeach = () => {
    seachForm.value.resetFields()
    doRefresh(1)
  }

  const exportExcelLists = () => {
    loading.value = true
    userCommon
      .downLoads('/app/complaint/list/export', form, '投诉列表')
      .then(() => {
        loading.value = false
      })
      .catch(() => {
        loading.value = false
      })
  }

  function onUpdateBorder(isBordered: boolean) {
    tables.bordered = isBordered
  }

  function onUpdateStriped(isStriped: boolean) {
    tables.striped = isStriped
  }

  function doRefresh(value?: number) {
    tables.tableLoading = true
    if (value) {
      pagination.page = value
    }
    get({
      url: '/app/complaint/list',
      data: {
        ...form,
        page: pagination.page,
        size: pagination.pageSize,
      },
    }).then((res) => {
      tables.tableLoading = false
      table.handleSuccess(res.data.list)
      pagination.setTotalSize((res.data as any).totalSize)
      return res.data.list
    })
  }

  //跟踪API开始时间限制结束时间
  function changeTraceEnd(time: any) {
    return time.getTime() > new Date(form.endTime).getTime() //大于结束时间的不选
  }
  //跟踪API结束时间限制开始时间
  function changeTraceStart(time: any) {
    return time.getTime() < new Date(form.addTime).getTime() //小于开始时间的不选
  }

  onMounted(async () => {
    await doRefresh()
  })
</script>

<style lang="less" scoped>
  :deep(.arco-table-cell-expand-icon) {
    justify-content: center;
  }

  .box {
    overflow: hidden; //超出的文本隐藏
    text-overflow: ellipsis; //用省略号显示
    white-space: nowrap;
  }

  .tabs {
    display: flex;
    justify-content: space-around;
    align-items: center;
    height: 40px;
    margin-bottom: 20px;

    .button {
      box-sizing: border-box;
      padding-bottom: 5px;
    }
  }

  .action {
    color: #009dff;
    border-bottom: 3px solid #009dff;
  }

  .butten {
    padding: 0px 4px;
  }
</style>
